package com.hashcoding.eyes.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.hashcoding.eyes.R;

public class SplashActivity extends AppCompatActivity implements Handler.Callback {

    private final int MSG_START_INIT = 0x01;
    private final int MSG_REQUEST_PERMISSION = 0x02;
    private final int MSG_START_MAIN = 0x03;

    private final int DELAY_TIME_MAX = 3000;
    private final int DELAY_TIME_MIN = 1000;

    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mHandler = new Handler(this);
        mHandler.sendEmptyMessage(MSG_REQUEST_PERMISSION);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_START_INIT:
                showGuideView();
                break;
            case MSG_REQUEST_PERMISSION:
                requestPermission();
                break;
            case MSG_START_MAIN:
                goNextView();
                return true;
        }
        return false;
    }

    private void showGuideView() {
        mHandler.sendEmptyMessageDelayed(MSG_START_MAIN, DELAY_TIME_MIN);
    }

    private void requestPermission() {
        mHandler.sendEmptyMessage(MSG_START_INIT);
    }

    private void goNextView() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
